<template>
  <div class="index page">
    <div class="header">
      <div class="swipe-box">
        <swiper
          class="swipe"
          indicator-color="rgba(245, 245, 245, 0.5)"
          indicator-active-color="#157AFE"
          :circular="true"
          :indicator-dots="true"
          :autoplay="true"
        >
          <swiper-item v-for="(item, index) in banner" :key="`banner${index}`">
            <div
              class="swipe-item justify-center"
              :style="{ 'background-image': `url(${item})` }"
              @tap="clickSwipe(item)"
            ></div>
          </swiper-item>
        </swiper>
      </div>
    </div>

    <!-- 分类栏目 -->
    <Types ref="types" />

    <!-- 新人专区 -->
    <NewUser ref="newUser" />

    <!-- 中部广告区 -->
    <CenterAd ref="centerAd" />

    <!-- 商品区 -->
    <goodsList ref="indexGoodsList" />
  </div>
</template>

<script>
import "./index.scss";
import Types from "./components/Types";
import NewUser from "./components/NewUser";
import CenterAd from "./components/CenterAd";
import goodsList from "./components/GoodsList";
export default {
  components: {
    Types,
    NewUser,
    CenterAd,
    goodsList,
  },
  data() {
    return {
      banner: [
        "https://cdn.uviewui.com/uview/swiper/swiper3.png",
        "https://cdn.uviewui.com/uview/swiper/swiper2.png",
        "https://cdn.uviewui.com/uview/swiper/swiper1.png",
        "https://cdn.uviewui.com/uview/swiper/swiper3.png",
        "https://cdn.uviewui.com/uview/swiper/swiper2.png",
        "https://cdn.uviewui.com/uview/swiper/swiper1.png",
      ],
      sessionId: "",
      flag: true,
      timeId: null,
    };
  },
  created() {
    // this.$taro.setNavigationBarTitle({
    //   title: this.$appName,
    // });
    // this.sessionId = this.$taro.getStorageSync("sessionId");
  },
  mounted() {
    this.getBanner();
    this.$checkLogin();
  },
  onPullDownRefresh() {
    this.getBanner();
    this.$refs.types.getTypes();
    this.$refs.newUser.getData();
    this.$refs.centerAd.getAd();
    this.$refs.indexGoodsList.getTabs();
  },
  onReachBottom() {
    this.$refs.indexGoodsList.getMoreData();
  },
  onShareAppMessage() {
    return {
      title: this.$appName,
      desc: "免押金租赁，首月0元租到手",
      path: "/pages/index/index",
    };
  },
  methods: {
    getBanner() {
      this.$api.getAdList({ position: 1 }).then((data) => {
        console.log(data, "=====");
        this.banner = data;
        this.$taro.stopPullDownRefresh();
      });
    },
    goSearch() {
      this.$goPage("/pagesIndex/searchData/index");
    },
    onOpenCard() {
      this.$http
        .request({
          url: "/alipayCard/queryCardUrl.json",
        })
        .then((data) => {
          var plugin = requirePlugin("alipassToolKit"); // 引用名称需与 app.json 中定义名称相同
          let params = {
            url: decodeURIComponent(data.data.applyCardUrl), //开卡链接，通过上文 获取投放链接 获取
            callback: function (res) {
              console.log("======", res);
              if (res.success === true) {
                console.log("----- success");
              }
            },
          };
          plugin.openCard(params); // 接口参数说明如下
        });
    },
    clickSwipe({ type, url, goodsId, appId, appPage }) {
      //  1、外部链接 2、图文详情 3、商品详情 4、小程序
      if (type == 1) {
        if (url.includes("alipays://")) {
          my.ap.navigateToAlipayPage({
            path: url,
            success: () => {
              console.log("99999999999999999999");
            },
            fail: (error) => {
              my.alert({ content: "失败：" + JSON.stringify(error) });
            },
          });
        } else {
          this.$goWebview(url);
        }
      } else if (type == 3) {
        this.$goGoodsDeail({ id: goodsId });
      } else if (type == 4) {
        my.navigateToMiniProgram({
          appId: appId,
          path: appPage,
          extraData: {
            j_f: "cjzyb_shengniu",
          },
        });
      }
    },
    fruitAction() {
      if (this.sessionId) {
        this.$goWebview(
          `https://h5.shengniuuz.com/cj/#/luck-draw?sessionId=${sessionId}`
        );
      } else {
        this.$goPage(`/pages/login/index}`);
      }
    },
  },
};
</script>
